﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;



namespace CitrixSystemVerifier
{
    public class CommandLine
    {
        [DllImport("kernel32.dll")]
        static extern bool AttachConsole(int dwProcessId);
        [DllImport("kernel32.dll")]
        static extern bool FreeConsole();

        private string m_File;
        public string File
        {
            get { return m_File; }
            set { m_File = value; }
        }

        private bool m_AutoClose = false;
        public bool AutoClose
        {
            get { return m_AutoClose; }
            set { m_AutoClose = value; }
        }

        private bool m_CheckInstallOnly = false;
        public bool CheckInstallOnly
        {
            get { return m_CheckInstallOnly; }
            set { m_CheckInstallOnly = value; }
        }

        private string m_ConfigurationFile;
        public string ConfigurationFile
        {
            get { return m_ConfigurationFile; }
            set { m_ConfigurationFile = value; }
        }

        private bool m_Verbose = false;
        public bool Verbose
        {
            get { return m_Verbose; }
            set { m_Verbose = value; }
        }

        public bool Process(string[] args)
        {
            bool Continue = true;

            if (args.Length > 0)
            {
                for (int i = 0; i < args.Length; i++)
                {
                    string flag = args[i].Substring(0, 2);
                    switch (flag)
                    {
                        case "-f":
                        case "-F":
                            {
                                File = args[i].Substring(2);
                            }
                            break;
                        case "-c":
                        case "-C":
                            {
                                ConfigurationFile = args[i].Substring(2);
                            }
                            break;
                        case "-a":
                        case "-A":
                            AutoClose = true;
                            break;
                        case "-i":
                        case "-I":
                            CheckInstallOnly = true;
                            break;
                        case "-v":
                        case "-V":
                            Verbose = true;
                            break;
                        case "-?":
                            {
                                AttachConsole(-1);
                                
                                Console.WriteLine("Command line options:\n" +
                                    "-F<File Name>  - Save to log file\n" +
                                    "-C<File Name>  - Configuration file\n" +
                                    "-A             - Automatically close application when done\n" +
                                    "-I             - Check Install Only (i.e. don't check processes)\n" + 
                                    "-V             - Verbose output");
                                FreeConsole();
                                Continue = false;
                            }
                            break;
                        default:
                            Console.WriteLine("Unsupported command line switch: " + flag);
                            break;
                    }
                    string Trace = i.ToString() + args[i];
                    Debug.WriteLine(Trace);
                }
            }
            return Continue;
        }
    }
}
